package com.skynet.whwpro.action;

import com.skynet.whwpro.Alipay.UserAuthController;
import com.skynet.whwpro.bean.BeanDyb;
import com.skynet.whwpro.bean.BeanJyjlb;
import com.skynet.whwpro.bean.BeanSjzcb;
import com.skynet.whwpro.utils.Utils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.filter.CrossOriginFilter;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;

import java.util.ArrayList;
import java.util.List;

@IocBean
@At("/company")
@Filters(@By(type = CrossOriginFilter.class))
public class ActionCompany {

    @Inject
    private Dao dao;


    @Inject
    private UserAuthController userauthcontroller;

    @At
    @Ok("json")
    public String test() {
        return "success";
    }

    /**
     * 新增公司
     *
     * @return
     */
    @At
    @Ok("json")
    public boolean addCompany(@Param("dpdata") String dpdata,
                              @Param("dydata") String dydata, @Param("cslx") String cslx) throws Exception {
        final boolean[] flag = {true};
        String dpid = Utils.getUuid();
        List<String> cslxlist = Json.fromJson(ArrayList.class, cslx);
        BeanSjzcb dp = Json.fromJson(BeanSjzcb.class, dpdata);
        dp.setWyid(dpid);
        Trans.exec(new Atom() {
            public void run() {
                try {
                    dao.insert(dp);
                    addUser(dydata, dpid);
                } catch (Exception e) {
                    e.printStackTrace();
                    flag[0] = false;
                }
            }
        });
        return flag[0];
    }

    @At
    @Ok("json")
    public Record addUser(@Param("dydata") String data,
                          @Param("wyid") String wyid){
        Record re = new Record();
        BeanDyb bean = Json.fromJson(BeanDyb.class, data);

        String uid = Utils.getUuid();
        if (Strings.isNotBlank(wyid)) {
            List<BeanDyb> dyblist = dao.query(BeanDyb.class, Cnd.where("xsdid", "=", wyid).and("dyuserid", "=", bean.getDyuserid()));
            if (dyblist != null && dyblist.size() > 0) {
                re.put("flag", false);
                re.put("msg", "用户已经是店员，请勿重复加入");
                return re;
            } else {
                try {
                    bean.setXsdid(wyid);
                    bean.setWyid(uid);
                    bean.setCbqx("1");
                    bean.setSmqx("1");
                    dao.insert(bean);
                }catch (Exception e){
                    e.printStackTrace();
                    re.put("flag", false);
                    re.put("msg", "保存员工有误");
                    return re;
                }

                re.put("flag", true);
                re.put("msg", "用户已添加为店员");
                return re;
            }

        } else {
            re.put("flag", false);
            re.put("msg", "参数有误");
            return re;
        }

    }

    @At
    @Ok("json")
    public List getCompanyDatas(@Param("dyuserid") String dyuserid) {
        if (Strings.isBlank(dyuserid)) {
            return new ArrayList();
        }
        String table = "(SELECT distinct(t2.*) FROM yhbzxcx_dyb t1 JOIN yhbzxcx_sjzcb t2 ON t1.xsdid = t2.wyid WHERE t1.dyuserid = '" +
                dyuserid + "') p";

        return dao.query(table, Cnd.where("p.zt", "=", "1").desc("p.pgrksj"));
    }

    @At
    @Ok("json")
    public QueryResult getSpList(@Param("page") int pageNum, @Param("rowsPerPage") int pageSize) {
        Pager pager = dao.createPager(pageNum, pageSize);
        Cnd cnd = Cnd.NEW();
        cnd.and("zt", "=", "1").desc("pgrksj");
        pager.setRecordCount(dao.count(BeanSjzcb.class, cnd));
        return new QueryResult(dao.query(BeanSjzcb.class, cnd, pager), pager);
    }
}
